﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Linq;
using WPFExcelReport.Helpers;
using System.Windows.Input;
using System.Windows.Controls;
using System.Collections.Generic;
namespace WPFExcelReport
{
    /// <summary>
    /// 专用于坯布检验
    /// 1.扫描布卷号，进入验布，读取生产单信息
    /// 2.输入长度，修改数据库记录
    /// 3.剪出新布卷
    /// 4.打印标签
    /// 5.疵点保存修正
    /// 6.事件记录
    /// 7.纬纱扣除
    /// 8.织轴扣除
    /// </summary>
    public partial class TFQI03
    {

        public TFQI03()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VWBBST();
            this.DataContext = MV;
            InitializeComponent();
        }
        public override void OnCOnlineChg(bool isonline)
        {
            if (!isonline)
                return;
            string key = cRK.KEY;
            cRK.KEY = "";
            cRK.KEY = key;
            base.OnCOnlineChg(isonline);
        }
        public override void CutFCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                if (cWB08.QTY == 0)
                {
                    throw new Exception("标签长度为0");

                }
                if (TMV.WBBSSTKLG == 0)
                {
                    throw new Exception("请输入落布长度");
                }
                base.CutFCommand(sender, e);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                Tracer.Error(ex.StackTrace, ex.Message);
            }
        }
        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            if (IsEdit)
            {
                SaveCommand(null, null);
                PM["QIMACHINE"] = MACHINEID;
                TMV.RVZEROWB(PM);
            }
            TBMain.Clear();
            base.QueryCommand(sender, e);
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            try
            {
                base.vwMaster_CurrentChanged(sender, e);
                if (dwMaster == null)
                {
                    WFART = "";
                    WAART = "";
                    DES = "";
                    EXORD = "";
                    PARTIE = "";
                    return;
                }
                if (TMV.WBBAUFTYP != "V")
                {
                    throw new Exception("必须留位到生产单而不是其他单");
                }

                if (TMV.WBBAUFLFD == "000000")
                {
                    throw new Exception("必须留位到生产单");
                }
                WFART = "F" + Helper.Left(dwMaster["WBBART"].ToString().PadRight(20), 20)
                        + Helper.Left(dwMaster["WBBVAR"].ToString().PadRight(2), 2)
                        + Helper.Left(dwMaster["WBBFARBE"].ToString().PadRight(10), 10)
                        + Helper.Left(dwMaster["WBBBESCHICH"].ToString().PadRight(10), 10)
                         + Helper.Left(dwMaster["WBBDESS"].ToString().PadRight(10), 10)
                        + Helper.Left(dwMaster["WBBCOLORIT"].ToString().PadRight(10), 10)
                           + "".PadRight(7)
                        + Helper.Left(Helper.ConvertGB2312To8859P1(dwMaster["WABEZ"].ToString().PadRight(20)), 20);
                WAART = WFART.Substring(0, 23);
                DES = Helper.Left(Helper.ConvertGB2312To8859P1(dwMaster["WABEZ"].ToString().PadRight(20)), 20);
                PARTIE = Helper.Left(dwMaster["PARTIE"].ToString().PadRight(10), 10);
                EXORD = Helper.Left(dwMaster["PRODORDER"].ToString().PadRight(11), 11)
                   + WFART
                    + Helper.Left(dwMaster["KDKUNDNA"].ToString().PadRight(8), 8)
                    + Helper.Left(dwMaster["LABELTYP"].ToString().PadRight(20), 20)
                    + Helper.Left(dwMaster["PARTIE"].ToString().PadRight(10), 10);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        ///剪出一定长度的面料，放入某个仓库
        ///
        public void LBCutCommand(object sender, RoutedEventArgs e)
        {
            string wh = WH;
            WH = "01LB01      ";
            CutFCommand(sender, e);
            WH = wh;
        }
    }
}
